//
//  ZNChartView.m
//  ZNChart
//
//  Created by Yim on 16/9/26.
//  Copyright © 2016年 LQ. All rights reserved.
//

#import "ZNChartView.h"
#import "UIView+LQExtension.h"
#import "ZNYAxisView.h"
#import "ZNXAxisView.h"
#import "ZNDraw.h"

@interface ZNChartView ()<UIScrollViewDelegate,ZNXAxisViewDelegate>
{
    NSMutableArray *_xViews;
    CGPoint _startPoint;
    CGPoint _endPoint;
    NSNumber *_firstValue;
    CAShapeLayer *_layer;
}

/**  *  模型    */
@property (nonatomic , strong) ZNDraw *draw;
/** *y轴 */
@property (nonatomic , weak) ZNYAxisView *yAxisView;
/** *x轴 */
@property (nonatomic , weak) ZNXAxisView *xAxisView;
/** *滚动视图 */
@property (nonatomic , weak) UIScrollView *scrollView;

/**  *  结束点数组    */
@property (nonatomic , strong) NSMutableArray *endPoints;

/**  *  图层数组    */
@property (nonatomic , strong) NSMutableArray *layers;

@end

@implementation ZNChartView

- (NSMutableArray *)endPoints
{
    if (!_endPoints) {
        _endPoints = [NSMutableArray array];
    }
    return _endPoints;
}

- (NSMutableArray *)layers
{
    if (!_layers) {
        _layers = [NSMutableArray array];
    }
    return _layers;
}


- (void)awakeFromNib {
    
    self.autoresizingMask = UIViewAutoresizingNone;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.textFont = [UIFont systemFontOfSize:12];
        self.axisColor = [UIColor blackColor];
        self.backgroundColor = [UIColor whiteColor];
        self.pillarColor = [UIColor greenColor];
        self.separateColor = [UIColor blackColor];
        self.lineColor = [UIColor brownColor];
        self.textColor = [UIColor blackColor];
        self.yAxisPercent = NO;
        self.xAxisPercent = NO;
        self.isShowPillar = NO;
        self.chartViewFill = NO;
        self.showSeparate = YES;
        self.showPointLabel = YES;
        self.showHeadAndTail = NO;
        self.isChartLine = YES;
        self.showPoint = YES;
        self.isRound = YES;
        self.isShowPillar = YES;
        self.showPointLabel = YES;
        self.pointGap = 50;
        self.xAxisTextGap = 10;
        self.yAxisTextGap = 10;
        self.yAxisNumber = 5;
        self.topMargin = 50;
        self.yAxisWidth = 50;
        self.yAxisMaxValue = 100;
        self.lineWidth = 1.0;
        self.yAxisTitle = @"标题";
        self.pageView = 1;
        
        
        [self setupUI];
    }
    return self;
}

- (void) setupUI
{
    
    ZNYAxisView *yAxisView = [[ZNYAxisView alloc] init];
    self.yAxisView = yAxisView;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    scrollView.delegate = self;
//    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
   
    [self addSubview:yAxisView];
    [self addSubview:scrollView];
}

- (void) setSubView
{
    self.yAxisView.x = 0;
    self.yAxisView.y = 0;
    self.yAxisView.width = self.yAxisWidth;
    self.yAxisView.height = self.height;
    
    self.yAxisView.xAxisTextGap = self.xAxisTextGap;
    self.yAxisView.yAxisMaxValue = self.yAxisMaxValue;
    self.yAxisView.yAxisNumber = self.yAxisNumber;
    self.yAxisView.topMargin = self.topMargin;
    self.yAxisView.yAxisTextGap = self.yAxisTextGap;
    self.yAxisView.axisColor = self.axisColor;
    self.yAxisView.percent = self.yAxisPercent;
    
    self.yAxisView.textColor = self.textColor;
    self.yAxisView.textFont = self.textFont;
    self.yAxisView.yAxisTitle = self.yAxisTitle;
    
    [self.yAxisView draw];
    
    
    self.scrollView.x = self.yAxisWidth;
    self.scrollView.y = 0;
    self.scrollView.width = self.width - self.yAxisWidth;
    self.scrollView.height = self.height;
    
    
}

- (void)setPoints:(NSArray *)points
{
    _points = points;
    [self setNeedsDisplay];
}


- (void)setXViews:(NSMutableArray *)xViews
{
    _xViews = xViews;
    
    [self setNeedsDisplay];
}

- (void)setPageView:(int)pageView
{
    _pageView = pageView;
    
    [self setNeedsDisplay];
}

- (void)setYAxisMaxValue:(CGFloat)yAxisMaxValue
{
    _yAxisMaxValue = yAxisMaxValue;
    
    [self setNeedsDisplay];
}



- (NSMutableArray *)xViews
{
    
    if (!_xViews) {
        _xViews = [NSMutableArray array];
    }
    return _xViews;
}

- (void)buttonClickEvent:(UIButton *)button andIndex:(NSInteger)index andView:(ZNXAxisView *)view
{
    if ([self.delegate respondsToSelector:@selector(buttonClickEvent:andIndex:andView:)]) {
        [self.delegate buttonClickEvent:button andIndex:index andView:view];
    }
}

- (void)drawRect:(CGRect)rect {
    
    [self setSubView];
    
    CGFloat textHeight = [@"x" sizeWithAttributes:@{NSFontAttributeName : self.textFont}].height ;
    CGFloat height = (self.height - textHeight - self.xAxisTextGap - self.topMargin);
    //    画箭头
    CGFloat yAxisHeight = self.height - textHeight - _xAxisTextGap;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 5, yAxisHeight - 4)];
    
    [path addLineToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width, yAxisHeight)];
    [path addLineToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 5, yAxisHeight + 4)];
    
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor blackColor].CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = 1.0;
    [self.layer addSublayer:layer];
    int i = 0;
    
    for (CAShapeLayer *lay in self.layers) {
        [lay removeFromSuperlayer];
    }
    
    [self.endPoints removeAllObjects];
    
    for (ZNXAxisView *view in self.scrollView.subviews) {
        [view removeFromSuperview];
    }
    self.scrollView.contentSize = CGSizeMake((self.points.count * self.pointGap + 5) * (self.pageView), self.height);
    for (int i = 0; i < self.pageView; i ++) {
        ZNXAxisView *xAxisView = [[ZNXAxisView alloc] init];
        self.xAxisView = xAxisView;
        self.xAxisView.delegate = self;
        self.xAxisView.x = i * (self.points.count * self.pointGap + 5);
        self.xAxisView.y = 0;
        CGFloat width = self.points.count * self.pointGap + 5;
        if (width < ([UIScreen mainScreen].bounds.size.width - self.yAxisWidth)) {
            self.xAxisView.width = [UIScreen mainScreen].bounds.size.width - self.yAxisWidth;
        }else
        {
            self.xAxisView.width = width;
        }
        self.xAxisView.height = self.height;
        xAxisView.tag = i;
        [self.xViews addObject:self.xAxisView];
        [self.scrollView addSubview:self.xAxisView];
    }
    
    
    for (ZNXAxisView *view in self.xViews) {
        i++;
        view.xAxisTextGap = self.xAxisTextGap;
        view.yAxisMaxValue = self.yAxisMaxValue;
        view.yAxisNumber = self.yAxisNumber;
        view.topMargin = self.topMargin;
        view.lineColor = self.lineColor;
        view.percent = self.xAxisPercent;
        view.textColor = self.textColor;
        view.textFont = self.textFont;
        view.axisColor = self.axisColor;
        view.pillarColor = self.pillarColor;
        view.showSeparate = self.showSeparate;
        view.separateColor = self.separateColor;
        view.lineWidth = self.lineWidth;
        view.isChartLine = self.isChartLine;
        view.pointGap = self.pointGap;
        view.showPoint = self.showPoint;
        view.isRound = self.isRound;
        view.isShowPillar = self.isShowPillar;
        view.showPointLabel = self.showPointLabel;
        view.points = self.points;
       
        view.pageView = self.pageView;
        [view setNeedsDisplay];
        
       
            NSNumber *lastValue = [view.points lastObject];
            
            CGPoint endPoint = CGPointMake((self.points.count * self.pointGap + 5) * i, height - lastValue.floatValue/self.yAxisMaxValue * height + self.topMargin);
            _endPoint = endPoint;
        
        
        [self.endPoints addObject:[NSValue valueWithCGPoint:endPoint]];
        
        
        NSNumber *firstValue = [view.points firstObject];
        if (i > 1) {
        _firstValue = firstValue;
        CGPoint startPoint = CGPointMake((self.points.count * self.pointGap + 5) * (i - 1) + self.pointGap, height - _firstValue.floatValue/self.yAxisMaxValue * height + self.topMargin);
        _startPoint = startPoint;
        }
        
        UIBezierPath *path = [[UIBezierPath alloc] init];
        
        if (i > 1) {
            [path moveToPoint:_startPoint];
           
            [path addLineToPoint:[self.endPoints[i - 2] CGPointValue]];
        }
        
        _layer = [[CAShapeLayer alloc] init];
        _layer.path = path.CGPath;
        _layer.strokeColor = self.lineColor.CGColor;
        _layer.fillColor = [UIColor clearColor].CGColor;
        _layer.lineWidth = self.lineWidth;
        
        [_layers addObject:_layer];
        [self.scrollView.layer addSublayer:_layer];
        
    }
    
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat width = self.points.count * self.pointGap + 5;
    if ([self.delegate respondsToSelector:@selector(scrollViewDidScroll:scrollViewGetWidth:andIndex:)]) {
        [self.delegate scrollViewDidScroll:scrollView scrollViewGetWidth:width andIndex:self.xAxisView.tag];
    }
    
    CGPoint point = scrollView.contentOffset;
    CGFloat x = point.x;
    
    CGSize size = scrollView.contentSize;
    float sd = size.width;
    NSLog(@"%f....%f",x,sd);
//    [scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
}

- (void)dealloc
{
    _xAxisView = nil;
    _yAxisView = nil;
    _scrollView = nil;
    self.xViews = nil;
}


@end
